package com.apinji.wxpay;

import com.apinji.common.plugin.Mutual;
import com.apinji.common.plugin.StrUtils;
import com.apinji.wxpay.dto.WxPayConsumeDto;
import com.apinji.wxpay.service.WxPayPayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 微信二维码支付，支付
 *
 */
@RestController
@EnableAutoConfiguration
@EnableAsync
@RequestMapping("/v1.0/wxpay")
public class WxPayPayController {

    @Autowired()
    private WxPayPayService wxPayPayService;

    /**
     * 微信二维码支付
     *
     * @param wxPayConsumeDto
     * @return
     */
    @RequestMapping(value = "/consume")
    @ResponseBody
    public Object consume(HttpServletRequest request, HttpServletResponse response, @RequestBody WxPayConsumeDto wxPayConsumeDto) throws Exception{
        if(StrUtils.isEmpty(wxPayConsumeDto.getUserUUID())) {
            return Mutual.invalidParam("无效参数[userUUID]").toString();
        }
        if(StrUtils.isEmpty(wxPayConsumeDto.getOrderId())) {
            return Mutual.invalidParam("无效参数[orderId]").toString();
        }
        if(StrUtils.isEmpty(wxPayConsumeDto.getOutOrderId())) {
            return Mutual.invalidParam("无效参数[outOrderId]").toString();
        }
        if(wxPayConsumeDto.getTxnAmt() <= 0) {
            return Mutual.invalidParam("支付金额不能少于0").toString();
        }
        if(StrUtils.isEmpty(wxPayConsumeDto.getOrderName())) {
            return Mutual.invalidParam("无效参数[orderName]").toString();
        }
        //
        return wxPayPayService.consume(wxPayConsumeDto, response, request);
    }




    @RequestMapping(value = "/backNotify")
    public void backRcvResponse(HttpServletRequest request, HttpServletResponse response) throws Exception {
        wxPayPayService.backNotify(request, response);
    }
}
